package com.ruoyi.monitor.utils.Common;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public final class DateUtils {
   private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
    public static String format(Date date){
        return simpleDateFormat.format(date) ;
    }
    public static String formatMD(Date date){
        return simpleDateFormat1.format(date) ;
    }


    /**
     * yyyy-MM-dd HH:mm:ss只需要年月日->yyyy-MM-dd
     * @param time
     * @return
     */
    public static String toStringMD(String time){
        String res = null;
        try {
            Date m= simpleDateFormat.parse(time);
            res = simpleDateFormat1.format(m);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * yyyy-MM-dd HH:mm:ss 转日期
     * @param time
     * @return
     */
    public static Date trans(String time){
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static Date getPastDate(int past,Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - past);
        Date today = calendar.getTime();
        return today;
    }

    /**
     * 去除毫秒的日期
     * @param date
     * @return
     */
    public static Date get0MIll(Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.MILLISECOND,0);
        return instance.getTime();
    }

    /**
     * 获取从现在开始的多少个小时的数组(预测用)
     * @param now
     * @param hours
     * @return
     */

    public static String[] getFromNow(Date now,Integer hours){
        String[] res = new String[hours];
        for (int i = 1; i <= hours; i++) {
            Date dateTime = DateUtil.offsetHour(now, i);
            res[i-1] = dateTime.toString();
//            System.out.println(res[i-1]);
        }
        return res;
    }



}
